package com.huanwei.cc.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.LimitLine;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.huanwei.cc.R;
import com.huanwei.cc.bean.ChengJi;
import com.huanwei.cc.utils.JSONUtils;
import com.huanwei.http.API;
import com.huanwei.http.Okhttp;

import java.util.ArrayList;
import java.util.List;

import butterknife.ButterKnife;

/**
 * Created by XP on 2015/2/15.
 * 成绩图表
 */
public class ZhuActivity extends AppCompatActivity {



    List<String> ccList = new ArrayList<String>();
    List<Integer> listdouble = new ArrayList<>();
    float[] ydata = new float[4];


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ccList.add("不及格");
        ccList.add("及格");
        ccList.add("良好");
        ccList.add("优秀");
        setContentView(R.layout.activity_zhu_);
        ButterKnife.bind(this);
//获取科目名字
        String kemu = getIntent().getStringExtra("kemu");
        //初始化图表
        barChart = (BarChart) findViewById(R.id.customBarChart1);
//        获取服务器数据
        getshuju(kemu);
    }

    public void getshuju(String KECHENG) {
            //根据科目获取服务器上图表的数据
        API.ChaXunChengJi(KECHENG, new Okhttp.Objectcallback() {
            @Override
            public void onsuccess(String st) {
                Log.e("111", "xx" + st);
                ChengJi chengJi = JSONUtils.parseJSON(st, ChengJi.class);
                List<ChengJi.DataBean> data = chengJi.getData();
                //计算下每个阶段的学生个数
                List<String> jisuan = JiSuan.jisuan(data);
                for (int i = 0; i < jisuan.size(); i++) {
//                    ydata[i] =   Float.parseFloat( jisuan.get(i));
                    listdouble.add(Integer.parseInt(jisuan.get(i)));
                }
//将数据部署在柱状图上
                show(listdouble);
            }

            @Override
            public void onFalia(int code, String errst) {
                Log.e("111", "网络错误" + errst);
            }
        });

    }
    private BarChart barChart;
    private YAxis leftAxis;             //左侧Y轴
    private YAxis rightAxis;            //右侧Y轴
    private XAxis xAxis;                //X轴
    private Legend legend;              //图例
    private LimitLine limitLine;

    public void show(List<Integer> val) {
//初始化图表
        initBarChart1( );
//        初始化数据
        showBarChart(val,"名称",getResources().getColor(R.color.blue));
    }
    final String[] xLabel = new String[]{"不及格", "及格", "良好", "优秀"};
    /**
     * 初始化柱状图1数据
     */
    private void initBarChart1( ) {

        /***图表设置***/
        //背景颜色
        barChart.setBackgroundColor(Color.WHITE);
        //不显示图表网格
        barChart.setDrawGridBackground(false);
        //背景阴影
        barChart.setDrawBarShadow(false);
        barChart.setHighlightFullBarEnabled(false);
        //显示边框
        barChart.setDrawBorders(true);
        //设置动画效果
//        barChart.animateY(1000,);
//        barChart.animateX(1000,);

        /***XY轴的设置***/
        //X轴设置显示位置在底部
        xAxis = barChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
//X轴自定义值
        xAxis.setValueFormatter(new IAxisValueFormatter() {
            @Override
            public String getFormattedValue(float value, AxisBase axis) {
                return ccList.get((int) value % ccList.size()) ;
            }
        });


        leftAxis = barChart.getAxisLeft();
        rightAxis = barChart.getAxisRight();
        //保证Y轴从0开始，不然会上移一点
//        leftAxis.setAxisMinimum(0f);
//        rightAxis.setAxisMinimum(0f);

        /***折线图例 标签 设置***/
        legend = barChart.getLegend();
        legend.setForm(Legend.LegendForm.LINE);
        legend.setTextSize(11f);
        //显示位置
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
        legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);
        //是否绘制在图表里面
        legend.setDrawInside(false);


        //不显示表边框
        barChart.setDrawBorders(false);
//        不显示右下角描述内容
        Description description = new Description();
        description.setEnabled(false);
        barChart.setDescription(description);
//        不显示X轴 Y轴线条
        xAxis.setDrawAxisLine(false);
        leftAxis.setDrawAxisLine(false);
        rightAxis.setDrawAxisLine(false);
        leftAxis.setEnabled(false);
    }
    public void showBarChart(List<Integer> dateValueList, String name, int color) {
        ArrayList<BarEntry> entries = new ArrayList<>();
        for (int i = 0; i < dateValueList.size(); i++) {
            /**
             * 此处还可传入Drawable对象 BarEntry(float x, float y, Drawable icon)
             * 即可设置柱状图顶部的 icon展示
             */
            BarEntry barEntry = new BarEntry(i, (float) dateValueList.get(i));
            entries.add(barEntry);
        }
        // 每一个BarDataSet代表一类柱状图
        BarDataSet barDataSet = new BarDataSet(entries, name);
        initBarDataSet(barDataSet, color);

        BarData data = new BarData(barDataSet);
        barChart.setData(data);
    }
    /**
     * 柱状图始化设置 一个BarDataSet 代表一列柱状图
     *
     * @param barDataSet 柱状图
     * @param color      柱状图颜色
     */
    private void initBarDataSet(BarDataSet barDataSet, int color) {
        barDataSet.setColor(color);
        barDataSet.setFormLineWidth(1f);
        barDataSet.setFormSize(15.f);
        //不显示柱状图顶部值
        barDataSet.setDrawValues(false);
//        barDataSet.setValueTextSize(10f);
//        barDataSet.setValueTextColor(color);
    }
}
